package com.example.oasystem.controller.process;


import com.example.oasystem.dao.process.AoaProcessListDaoOfChen;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.process.*;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.impl.ProcessListServiceImplOfChen;


import com.example.oasystem.utils.ConvertUpMoneyUtil;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProcessManageController {
    @Autowired
    ProcessListServiceImplOfChen processListServiceImplOfChen;
    @Autowired
    AoaProcessListDaoOfChen processListDaoOfChen;
    //初次进入
    @RequestMapping("audit")//重了
    public String audit(HttpSession session, Model model,

                                  @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                                    @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                                  @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        AoaUser user = (AoaUser) session.getAttribute("user");
        PageInfo<ProcessDTOOfChen> allProcess = processListServiceImplOfChen.getProcessLimit(pageNum, pageSize, user.getUserName(),baseKey);
        Page page=new Page(allProcess.getSize(), Math.toIntExact(allProcess.getTotal()),allProcess.getPages(),allProcess.getPageNum(),allProcess.isIsFirstPage(),allProcess.isIsLastPage());
        List<ProcessDTOOfChen> prolist = allProcess.getList();
        for (ProcessDTOOfChen processDTOOfChen : prolist) {
            System.out.println("processDTOOfChen = " + processDTOOfChen);
        }
        System.out.println("进入和audit");
        System.out.println(page);
        System.out.println(pageNum);
        System.out.println(baseKey);
        model.addAttribute("page",page);
        model.addAttribute("prolist",prolist);
        model.addAttribute("url","getAuditPage");
        model.addAttribute("baseKey",baseKey);
        return "process/auditing";
    }
    //展示页面的分页
    @RequestMapping("getAuditPage")
    public String getAuditPage(HttpSession session, Model model,
                       @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                        @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                        @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        AoaUser user = (AoaUser) session.getAttribute("user");
        PageInfo<ProcessDTOOfChen> allProcess = processListServiceImplOfChen.getProcessLimit(pageNum, pageSize, user.getUserName(),baseKey);
        Page page=new Page(allProcess.getSize(), Math.toIntExact(allProcess.getTotal()),allProcess.getPages(),allProcess.getPageNum(),allProcess.isIsFirstPage(),allProcess.isIsLastPage());
        List<ProcessDTOOfChen> prolist = allProcess.getList();

        /*        List<ProcessDTOOfChen> statusIds = processService.getStatusId(user.getUserId());*/

/*
        model.addAttribute("statusIds",statusIds);*/
        for (ProcessDTOOfChen processDTOOfChen : prolist) {
            System.out.println("processDTOOfChen = " + processDTOOfChen);
        }
        model.addAttribute("page",page);
        model.addAttribute("prolist",prolist);
        model.addAttribute("url","getAuditPage");
        model.addAttribute("baseKey",baseKey);
        return "process/audtable";
    }
    //展示
    @RequestMapping("particular")
    public String particular(HttpSession session, Model model,
                             @RequestParam(value = "id")int id,
                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                             @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                             @RequestParam(value = "typeName")String typeName){

        System.out.println("typename = " + typeName);
        AoaProcessListOfChen findeve = processListServiceImplOfChen.findeve(id);
        AoaTypeList harry=processListDaoOfChen.GetHary(id);


        if(typeName.equals("出差申请")){

            System.out.println("findeve = " + findeve);
            model.addAttribute("map",findeve);
            model.addAttribute("hary",harry);
            model.addAttribute("baseKey",baseKey);
            model.addAttribute("pageNum",pageNum);

            String tu= null;
            try {
                AoaAttachmentList tupian=processListDaoOfChen.getTupian(Math.toIntExact(findeve.getProFileId()));
                System.out.println(tupian);
                tu = tupian.getAttachmentPath();
            } catch (Exception e) {
                e.printStackTrace();
            }


            model.addAttribute("tupian",tu);
            return "process/eveserach";
        }
        else if(typeName.equals("请假申请"))
        {
            AoaProcessListOfChen holiday=processListDaoOfChen.getHo(id);
            System.out.println("holiday = " + holiday);
            model.addAttribute("ho",holiday);
            model.addAttribute("map",findeve);
            model.addAttribute("hary",harry);
            model.addAttribute("baseKey",baseKey);
            model.addAttribute("pageNum",pageNum);
            String tu= null;
            try {
                AoaAttachmentList tupian=processListDaoOfChen.getTupian(Math.toIntExact(findeve.getProFileId()));
                System.out.println(tupian);
                tu = tupian.getAttachmentPath();
            } catch (Exception e) {
                e.printStackTrace();
            }

            model.addAttribute("tupian",tu);
            return "process/holiserch";
        }
        else if(typeName.equals("出差费用"))
        {
            AoaProcessListOfChen holiday=processListDaoOfChen.getMon(id);
            System.out.println("holiday = " + holiday);
            List<tralist> trlist=processListDaoOfChen.getTralist(Math.toIntExact(holiday.getEvectionmoney().getEvectionmoneyId()));
            Double allMoneyTt=0.0;
            for (tralist tralist : trlist) {
                allMoneyTt+=tralist.getTrafficMoney();
            }
            List<AoaStay> stlist=processListDaoOfChen.getStay(id);
            Double allMoneySt=0.0;

            for (AoaStay aoaStay : stlist) {
                Double geren=0.0;
                geren=aoaStay.getDay()*aoaStay.getStayMoney();
                allMoneySt+=geren;
            }
            Double allMoney=0.0;
             allMoney=allMoneySt+allMoneyTt;
            String Money=ConvertUpMoneyUtil.numberConvertAmount(allMoney+"");
            System.out.println(allMoney);
            System.out.println(Money);
            model.addAttribute("baseKey",baseKey);
            model.addAttribute("pageNum",pageNum);
            model.addAttribute("tralist",trlist);
            model.addAttribute("staylist",stlist);
            model.addAttribute("tramoney",allMoneyTt);
            model.addAttribute("staymoney",allMoneySt);
            model.addAttribute("allMoney",Money);
            model.addAttribute("mon",holiday);
            model.addAttribute("map",findeve);
            model.addAttribute("hary",harry);
            return "process/evemonserch";
        }
        else if(typeName.equals("离职申请"))
        {

            AoaProcessListOfChen lizhi =processListDaoOfChen.getLizhi(id);//离职

            System.out.println("lizhi = " + lizhi);
            model.addAttribute("baseKey",baseKey);
            model.addAttribute("pageNum",pageNum);
            model.addAttribute("lizhi",lizhi);
            model.addAttribute("map",findeve);
            model.addAttribute("hary",harry);
            return "process/resserch";
        }
        else if(typeName.equals("转正申请"))
        {
            AoaProcessListOfChen zhuanzheng =processListDaoOfChen.getZhuanzheng(id);//离职
            System.out.println("zhuanzheng = " + zhuanzheng);
            model.addAttribute("baseKey",baseKey);
            model.addAttribute("pageNum",pageNum);
            model.addAttribute("zhuanzheng",zhuanzheng);
            model.addAttribute("map",findeve);
            model.addAttribute("hary",harry);
            return "process/reguserch";
        }
        else if(typeName.equals("加班申请"))
        {
            AoaProcessListOfChen jiaban =processListDaoOfChen.getJiaban(id);//离职
            System.out.println("jiaban = " + jiaban);
            model.addAttribute("baseKey",baseKey);
            model.addAttribute("pageNum",pageNum);
            model.addAttribute("jiaban",jiaban);
            model.addAttribute("map",findeve);
            model.addAttribute("hary",harry);
            return "process/overserch";
        }
        else if(typeName.equals("费用报销")){
            baoxiaoDTO baoxiao =processListDaoOfChen.getFeiyongBaoxiao(id);
            List<details> details=processListDaoOfChen.getDetail(id);
            System.out.println("现在时报销"+baoxiao);
            String money=ConvertUpMoneyUtil.numberConvertAmount(baoxiao.getAllMoney()+"");
            System.out.println("baoxiao = " + baoxiao);
            String tu =null;
            if (baoxiao.getAttachmentPath()!=null)
            {
                AoaAttachmentList tupian=processListDaoOfChen.getTupian(Math.toIntExact(findeve.getProFileId()));
                System.out.println(tupian);
                 tu=tupian.getAttachmentPath();
            }

            model.addAttribute("tupian",tu);
            model.addAttribute("baseKey",baseKey);
            model.addAttribute("pageNum",pageNum);
            model.addAttribute("detaillist",details);
            model.addAttribute("money",money);
            model.addAttribute("baoxiao",baoxiao);
            model.addAttribute("map",findeve);
            model.addAttribute("hary",harry);
            return "process/serch";
        }
        return "process/auditing";
    }
    //删除
    @RequestMapping("sdelete")
    public String sdelete(Model model,HttpSession session,
                          @RequestParam(value = "id")int pid,
                          @RequestParam(value = "baseKey",defaultValue = "")String baseKey,
                          @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                          @RequestParam(value = "pageSize",defaultValue = "10")int pageSize,
                          @RequestParam(value = "typeName",defaultValue = "")String type){
        AoaUser user =(AoaUser)session.getAttribute("user");
        String typeName = "";
        switch (type) {
            case "费用报销":
                typeName = "aoa_bursement";
                break;
            case "出差费用":
                typeName = "aoa_evectionmoney";
                break;
            case "出差申请":
                typeName = "aoa_evection";
                break;
            case "加班申请":
                typeName = "aoa_overtime";
                break;
            case "请假申请":
                typeName = "aoa_holiday";
                break;
            case "转正申请":
                typeName = "aoa_regular";
                break;
            case "离职申请":
                typeName = "aoa_resign";
                break;
        }
        //删除每个指定type表中的数据

        //删除reviewed表中的数据
        processListDaoOfChen.DeleteProcess(Math.toIntExact(user.getUserId()),pid);
        //根据pid获取shenuser字段
        String newshenuser = "";
        String shenUsers = processListDaoOfChen.getShenUsers(pid);
        String[] split = shenUsers.split(";");
        if (split.length>1){
            //在shenuser字段中用''来替换username
            newshenuser = shenUsers.replaceAll(user.getUserName()+";","");
            System.out.println("第一次"+newshenuser);
            newshenuser = newshenuser.replaceAll(user.getUserName(),"");
            System.out.println("第二次"+newshenuser);
            processListDaoOfChen.UpdateByName(pid,newshenuser);
        }else if(split.length == 1){
            processListDaoOfChen.DeleteByname(pid);
            processListDaoOfChen.DeleteByType(pid,typeName);
        }
        model.addAttribute("success","删除成功");
        model.addAttribute("pageNum",pageNum);
        return "forward:/audit?pageNum="+pageNum;
    }
}
